/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DatabaseConnectivity;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.Application;
import javax.persistence.Version;

/**
 *
 * @author Ekantik
 */
public class MySQLConnectivity {

    public String getSuccess() {
        return success;
    }

    public void setSuccess(String success) {
        this.success = success;
    }


    private Connection connect = null;
    private Statement statement = null;
    private ResultSet resultSet = null;
    private String query = "Select * from ratings";
    private String success;

    public void getSelectTable(String query){
        try{
            this.setResultSet(this.getStatement().executeQuery(query));
        }
        catch(SQLException ex){
            this.log(ex);
        }
    }
    public String getQuery() {
        return query;
    }
    public MySQLConnectivity getMySQLConnectivity(){
        return new MySQLConnectivity();
    }

    public void setQuery(String query) {
        this.query = query;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
    String db;

    public String getDb() {
        return db;
    }

    public void setDb(String db) {
        this.db = db;
    }
    String url = "jdbc:mysql://localhost:3306/ratingmatrix";
    String user = "root";
    String password =null;
    
    public void selectQuery(){
        try{
        this.getStatement().executeQuery(query);
        }
        catch(SQLException ex){
            this.log(ex);
        }
    }
    public void insertIntoTable(){
        try{
            int i = getStatement().executeUpdate(query);
            if( i == 1){
                    System.out.println("1 row Successfully Added");
            }
        }
        catch(SQLException ex){
            this.log(ex);
        }
    }
    public MySQLConnectivity() {
       try{
           
           setConnect(DriverManager.getConnection(url,user,password));
           this.setSuccess("Connection Successful.");
           setStatement(getConnect().createStatement());
           
           
           
       }catch(SQLException ex){
           this.log(ex);
       } 
        
    }
    public MySQLConnectivity(String db){
    try{
        url = "jdbc:mysql://localhost:3306/"+db;
        setConnect(DriverManager.getConnection(url,user,password));
        this.setSuccess("Connection Successful.");
        setStatement(getConnect().createStatement());
                   
       }catch(SQLException ex){
           this.log(ex);
       } 
    }
    
    public String test(){
        return "Object Successfully createad";
    }
    public void closeConnection(){
        try{
            resultSet.close();
            statement.close();
            connect.close();
        }
        catch(SQLException ex){
            this.log(ex);
        }
    }
    public void log(SQLException ex){
        Logger lgr = Logger.getLogger(Version.class.getName());
        lgr.log(Level.SEVERE, ex.getMessage(), ex);
    }
    public Connection getConnect() {
        return connect;
    }

    public void setConnect(Connection connect) {
        this.connect = connect;
    }

    public Statement getStatement() {
        return statement;
    }

    public void setStatement(Statement statement) {
        this.statement = statement;
    }

    public ResultSet getResultSet() {
        return resultSet;
    }

    public void setResultSet(ResultSet resultSet) {
        this.resultSet = resultSet;
    }

/*    public static void main(String args[]){
        MySQLConnectivity msc = new MySQLConnectivity();
        msc.insertIntoTable();
        try{
            
        if(msc.resultSet.next()){
            System.out.println(msc.resultSet.getString(1));
        }
        }catch(SQLException ex){
            msc.log(ex);
        }
    }*/
}
